﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Modulo.Seguridad.Base
{
    public partial class WFAModulos : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        private bool guardo = false;
        public WFAModulos()
        {
            InitializeComponent();
        }

        private void TsbSalir_Click(object sender, EventArgs e)
        {
            if (guardo == true)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                DialogResult = DialogResult.Cancel;
            }
        }

        private void TsbGuardar_Click(object sender, EventArgs e)
        {
            ControlError.SetError(TxtCodigo, "");
            if (string.IsNullOrEmpty(TxtCodigo.Text))
            {
                ControlError.SetError(TxtCodigo, "Ingrese un código");
                return;
            }
            //Grabar
            try
            {
                EBModulo mod = new EBModulo();
                mod.Codigo = TxtCodigo.Text;
                mod.Descripcion = TxtDescripcion.Text;
                mod.Grabar();
                guardo = true;
                TxtCodigo.Text = "";
                TxtDescripcion.Text = "";
                Cargar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        protected void Cargar()
        {
            GrdModulos.DataSource = null;
            LBModulo lmod = new LBModulo();
            lmod.Buscar();
            if (lmod.Count > 0)
            {
                GrdModulos.DataSource = lmod;
                TsbEliminar.Enabled = true;
                BarraMensaje.Text = "Se encontraron (" + lmod.Count + ") registro(s)";
            }
            else
            {
                TsbEliminar.Enabled = false;
            }
        }

        private void WFAModulos_Load(object sender, EventArgs e)
        {
            TxtCodigo.CharacterCasing = CharacterCasing.Upper;
            TsbEliminar.Enabled = false;
            TsbGuardar.Enabled = false;
            Cargar();
        }

        private void TsbEliminar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Desea realmente eliminar el registro?", "Información", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    EBModulo mod = new EBModulo();
                    mod.Codigo = GrdModulos.CurrentRow.Cells[0].Value.ToString();
                    mod.Eliminar();
                    guardo = true;
                    Cargar();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void TxtCodigo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (TxtCodigo.Text.Length > 0)
            {
                TsbGuardar.Enabled = true;
            }
            else
            {
                TsbGuardar.Enabled = false;
            }
        }

        private void TsbModificar_Click(object sender, EventArgs e)
        {
            EBModulo Modulo = (EBModulo)GrdModulos.CurrentRow.DataBoundItem;
            WFModulos_Edit mod = new WFModulos_Edit(Modulo);
            if (mod.ShowDialog() == DialogResult.OK)
            {
                Cargar();
                guardo = true;
            }
        }
    }
}
